/**
 * File Name : AndroidGraphics.java
 * <p>
 * Author : maidou
 * <p>
 * Create Time : 2012-11-15, 下午11:37:22
 * <p>
 */

package com.ttomato.game.framework.impl;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.graphics.Path.FillType;

import com.ttomato.game.framework.Graphics;
import com.ttomato.game.framework.Pixmap;

/**
 * Add the description for the file here.
 * <p>
 */
public class AndroidGraphics implements Graphics
{
    AssetManager mAssetManager;

    Bitmap mFrameBuffer;

    Canvas mCanvas;

    Paint mPaint;

    Rect mSrcRect = new Rect();

    Rect mDstRect = new Rect();

    public AndroidGraphics(AssetManager assetManager, Bitmap frameBuffer)
    {
        mAssetManager = assetManager;
        mFrameBuffer = frameBuffer;
        mCanvas = new Canvas(mFrameBuffer);
        mPaint = new Paint();
    }

    @Override
    public Pixmap newPixmap(String fileName, PixmapFormat format)
    {
        Config config = null;
        if (format == PixmapFormat.RGB565)
        {
            config = Config.RGB_565;
        }
        else if (format == PixmapFormat.ARGB4444)
        {
            config = Config.ARGB_4444;
        }
        else
        {
            config = Config.ARGB_8888;
        }

        Options options = new Options();
        options.inPreferredConfig = config;

        InputStream inputStream = null;
        Bitmap bitmap = null;

        try
        {
            inputStream = mAssetManager.open(fileName);
            bitmap = BitmapFactory.decodeStream(inputStream);

            if (null == bitmap)
            {
                throw new RuntimeException("Couldn't load bitmap from asset "
                        + fileName);
            }
        } catch (IOException e)
        {
            throw new RuntimeException("Couldn't load bitmap from asset "
                    + fileName);
        } finally
        {
            if (null != inputStream)
            {
                try
                {
                    inputStream.close();
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        if (bitmap.getConfig() == Config.RGB_565)
        {
            format = PixmapFormat.RGB565;
        }
        else if (bitmap.getConfig() == Config.ARGB_4444)
        {
            format = PixmapFormat.ARGB4444;
        }
        else
        {
            format = PixmapFormat.ARGB888;
        }

        return new AndroidPixmap(bitmap, format);
    }

    @Override
    public void clear(int color)
    {
        mCanvas.drawRGB((color & 0xff0000) >> 16,
                (color & 0x00ff00) >> 8,
                (color & 0x0000ff));
    }

    @Override
    public void drawPixel(int x, int y, int color)
    {
        mPaint.setColor(color);
        mCanvas.drawPoint(x, y, mPaint);
    }

    @Override
    public void drawLine(int x, int y, int x2, int y2, int color)
    {
        mPaint.setColor(color);
        mCanvas.drawLine(x, y, x2, y2, mPaint);
    }

    @Override
    public void drawRect(int x, int y, int width, int height, int color)
    {
        mPaint.setColor(color);
        mPaint.setStyle(Style.FILL);
        mCanvas.drawRect(x, y, x + width - 1, y + height - 1, mPaint);
    }

    @Override
    public void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY,
            int srcWidth, int srcHeight)
    {
        mSrcRect.left = srcX;
        mSrcRect.top = srcY;
        mSrcRect.right = srcX + srcWidth - 1;
        mSrcRect.bottom = srcY + srcHeight - 1;

        mDstRect.left = x;
        mDstRect.top = y;
        mDstRect.right = x + srcWidth - 1;
        mDstRect.bottom = y + srcHeight - 1;

        mCanvas.drawBitmap(((AndroidPixmap) pixmap).mBitmap, mSrcRect, mDstRect, null);
    }

    @Override
    public void drawPixmap(Pixmap pixmap, int x, int y)
    {
        mCanvas.drawBitmap(((AndroidPixmap) pixmap).mBitmap, x, y, null);
    }

    @Override
    public int getWidth()
    {
        return mFrameBuffer.getWidth();
    }

    @Override
    public int getHeight()
    {
        return mFrameBuffer.getHeight();
    }

}
